import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import zhlang from "@/lang/zh.json"
import enlang from "@/lang/en.json"
// 获取本地存储的语言
const getLocalLanguage = (): string => {
  return localStorage.getItem("i18n") || "zh"
}
i18n
  .use(initReactI18next)
  .init({
    resources: {
      zh: {
        translation: zhlang
      },
      en: {
        translation: enlang
      }
    },
    lng: getLocalLanguage(),
    fallbackLng: "zh"
  })
export default i18n
// 获取当前语言的方法
export const getLanguage = (): string => {
  return i18n.language
}
// 切换语言的方法
export const changeLanguage = (lang: string) => {
  i18n.changeLanguage(lang)
  localStorage.setItem("i18n", lang)
}
// 支持什么语言的方法
export const getLanguageData = () => {
  return [
    {
      code: "zh",
      name: "中文"
    },
    {
      code: "en",
      name: "English"
    }
  ]
}